class Solution(object):
    def connect(self, root):
        """
        :type root: TreeLinkNode
        :rtype: nothing
        """
        queue = [root] if root else []
        while queue:
            newQueue = []
            for i,r in enumerate(queue):
                r.next = queue[i+1] if i+1<len(queue) else None
                if r.left: newQueue.append(r.left)
                if r.right: newQueue.append(r.right)
            queue = newQueue